// pages/gongyiguanli/index.js
const db = wx.cloud.database();
const { formatTime } = require("../../utils/index");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {},
    autoHeight: false,
    error: '',
    loading: false,
    image: {},
    checked: false,
    count: 0,
    rules: [{
        name: 'worker',
        rules: {
          required: true,
          message: '必填职位'
        }
      },
      {
        name: 'xinzi',
        rules: {
          required: true,
          message: '必填薪资'
        }
      },
      {
        name: 'xueli',
        rules: {
          required: true,
          message: '必填学历'
        }
      },
      {
        name: 'jingyan',
        rules: {
          required: true,
          message: '必填经验'
        }
      },
      {
        name: 'renshu',
        rules: {
          required: true,
          message: '必填人数'
        }
      }
    ]
  },

  formInputChange(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value
    })
  },
  submitForm() {
    this.selectComponent('#form').validate((valid, errors) => {
      console.log('valid', valid, errors)
      if (!valid) {
        const firstError = Object.keys(errors)
        if (firstError.length) {
          this.setData({
            error: errors[firstError[0]].message
          })
        }
      } else {
        this.submit(this.data.formData)
      }
    })
  },

  submit(params) {
    this.setData({
      loading: true
    })
    params.create_time = formatTime(new Date());
    params.create_at = new Date()
    db.collection("zhaopin").add({
      data: params
    }).then(res => {
      console.log(res);
      this.setData({
        loading: false
      })
      wx.navigateTo({
        url: '/pages/success/index?url=zhaopinguanli',
      })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})